package com.lyy.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.lyy.entity.Chapter;
import com.lyy.mapper.ChapterMapper;
import com.lyy.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Service
@CacheConfig(cacheNames = "lesson:cache:chapter")
public class ChapterServiceImpl implements ChapterService {
    @Autowired
    private ChapterMapper chapterMapper;

    @Cacheable(key = "#chapterId", condition = "#chapterId != null")
    @Override
    public Chapter selectByChapterId(Integer chapterId) {

        // DB: 按章主键查询一条章记录
        Chapter condition = new Chapter();
        condition.setChapterId(chapterId);
        List<Chapter> chapters = chapterMapper.select(condition);
        if (ObjectUtil.isEmpty(chapters)){
            throw new RuntimeException("章查询失败: 章主键不存在");
        }

        return chapters.get(0);
    }

}
